#include "dhsdk.h"
#include "dhnetsdk.h"

extern "C" {
#include "log.h"
#include "queue.h"
#include "common.h"
}

static BOOL g_bNetSDKInitFlag = FALSE;
static LLONG g_lRealLoadHandle = 0;
Queue_t *g_trafficjunction_info_queue = NULL;

// 常用回调集合定义
void CALLBACK DisConnectFunc(LLONG lLoginID, char *pchDVRIP, int nDVRPort, LLONG dwUser)
{
}

void CALLBACK HaveReConnect(LLONG lLoginID, char *pchDVRIP, int nDVRPort, LLONG dwUser)
{
}

int AnalyzerDataCallBack(LLONG lAnalyzerHandle, DWORD dwAlarmType, void *pAlarmInfo, BYTE *pBuffer, DWORD dwBufSize, LDWORD dwUser, int nSequence, void *reserved)
{
    printf("AnalyzerDataCallBack: lAnalyzerHandle[%d], dwAlarmType[%d]\n", lAnalyzerHandle, dwAlarmType);
    if (lAnalyzerHandle == g_lRealLoadHandle && dwAlarmType == EVENT_IVS_TRAFFICJUNCTION) {
        // 处理数据
        DEV_EVENT_TRAFFICJUNCTION_INFO *pstTrafficJunctionInfo = (DEV_EVENT_TRAFFICJUNCTION_INFO *)pAlarmInfo;
        // 处理数据
        trafficjunction_info_t info;
        info.event_time = get_current_time_in_milliseconds();
        info.lane_id = pstTrafficJunctionInfo->stTrafficCar.nLane;
        snprintf(info.plate, sizeof(info.plate), "%s", pstTrafficJunctionInfo->stTrafficCar.szPlateNumber);
        info.speed = pstTrafficJunctionInfo->stTrafficCar.nSpeed * 10;
        snprintf(info.plate_color, sizeof(info.plate_color), "%s", pstTrafficJunctionInfo->stTrafficCar.szPlateColor);
        snprintf(info.vehicle_color, sizeof(info.vehicle_color), "%s", pstTrafficJunctionInfo->stTrafficCar.szVehicleColor);
        snprintf(info.obj_type, sizeof(info.obj_type), "%s", pstTrafficJunctionInfo->stuObject.szObjectType);
        // 将数据放入队列
        queue_enqueue(g_trafficjunction_info_queue, &info);
    }
    return 0;
}

void dhsdk_init()
{
    INIT_QUEUE(g_trafficjunction_info_queue, 20, sizeof(trafficjunction_info_t), "g_trafficjunction_info_queue");

    // 初始化SDK
    g_bNetSDKInitFlag = CLIENT_Init(DisConnectFunc, 0);
    if (FALSE == g_bNetSDKInitFlag) {
        printf("Initialize client SDK fail; \n");
        return;
    }
    else {
        printf("Initialize client SDK done; \n");
    }

    CLIENT_SetAutoReconnect(HaveReConnect, 0);

    // 设置登录超时时间和尝试次数
    // 此操作为可选操作
    int nWaitTime = 5000;  // 登录请求响应超时时间设置为 5s
    int nTryTimes = 3;     // 登录时尝试建立链接3次
    CLIENT_SetConnectTime(nWaitTime, nTryTimes);

    NET_IN_LOGIN_WITH_HIGHLEVEL_SECURITY stInparam;
    memset(&stInparam, 0, sizeof(stInparam));
    stInparam.dwSize = sizeof(stInparam);
    strncpy(stInparam.szIP, "52.93.86.35", sizeof(stInparam.szIP) - 1);
    strncpy(stInparam.szPassword, "Glsd@2025", sizeof(stInparam.szPassword) - 1);
    strncpy(stInparam.szUserName, "admin", sizeof(stInparam.szUserName) - 1);
    stInparam.nPort = 37777;
    stInparam.emSpecCap = EM_LOGIN_SPEC_CAP_TCP;

    NET_OUT_LOGIN_WITH_HIGHLEVEL_SECURITY stOutparam;
    memset(&stOutparam, 0, sizeof(stOutparam));
    stOutparam.dwSize = sizeof(stOutparam);

    LLONG lLoginID = CLIENT_LoginWithHighLevelSecurity(&stInparam, &stOutparam);
    if (0 == lLoginID) {
        printf("Login Device[%s] Port[%d] Failed. Last Error[%x]\n", stInparam.szIP, stInparam.nPort, CLIENT_GetLastError());
    }
    else {
        printf("Login Device[%s] Port[%d] Success. \n", stInparam.szIP, stInparam.nPort);
        LDWORD dwUser = 0;
        g_lRealLoadHandle = CLIENT_RealLoadPictureEx(lLoginID, 0, EVENT_IVS_TRAFFICJUNCTION, TRUE, AnalyzerDataCallBack, dwUser, NULL);
        if (0 == g_lRealLoadHandle) {
            printf("CLIENT_RealLoadPictureEx Failed!Last Error[%x]\n", CLIENT_GetLastError());
        }
    }
}